{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5375f3ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlwings as xw\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f379545",
   "metadata": {},
   "source": [
    "### 启动Excel APP并打开Excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "36b16a01",
   "metadata": {},
   "outputs": [],
   "source": [
    "app = xw.App(visible=False, add_book=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "53f00953",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook = app.books.open(\"成绩表数据.xlsx\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b49ed6c8",
   "metadata": {},
   "source": [
    "### 读取主表数据：成绩表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a8318189",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_total = (\n",
    "    workbook\n",
    "    .sheets[0]\n",
    "    .range(\"A1\")\n",
    "    .options(pd.DataFrame, expand='table', index=False, numbers=int)\n",
    "    .value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03524f38",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>成绩</th>\n",
       "      <th>班级</th>\n",
       "      <th>电话号码</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>张三</td>\n",
       "      <td>90</td>\n",
       "      <td>一班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>里斯</td>\n",
       "      <td>80</td>\n",
       "      <td>二班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>王五</td>\n",
       "      <td>78</td>\n",
       "      <td>二班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>马路</td>\n",
       "      <td>98</td>\n",
       "      <td>三班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>枸杞</td>\n",
       "      <td>96</td>\n",
       "      <td>四班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>走吧</td>\n",
       "      <td>95</td>\n",
       "      <td>五班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>高飞</td>\n",
       "      <td>88</td>\n",
       "      <td>一班</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   学号  姓名  成绩  班级  电话号码\n",
       "0   1  张三  90  一班  None\n",
       "1   2  里斯  80  二班  None\n",
       "2   3  王五  78  二班  None\n",
       "3   4  马路  98  三班  None\n",
       "4   5  枸杞  96  四班  None\n",
       "5   6  走吧  95  五班  None\n",
       "6   7  高飞  88  一班  None"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_total"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76040342",
   "metadata": {},
   "source": [
    "### 读取各个班级的学生数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5fbfe535",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_student_list = []\n",
    "\n",
    "for sheet in list(workbook.sheets)[1:]:\n",
    "    class_ = {\"1班同学录\":\"一班\", \n",
    "              \"2班同学录\":\"二班\", \n",
    "              \"3班同学录\":\"三班\", \n",
    "              \"4班同学录\":\"四班\", \n",
    "              \"5班同学录\":\"五班\"}[sheet.name]\n",
    "    df_student = (\n",
    "        sheet.range(\"A1\")\n",
    "        .options(pd.DataFrame, expand='table', index=False, numbers=int)\n",
    "        .value)\n",
    "    df_student[\"班级\"] = class_\n",
    "    df_student_list.append(df_student)\n",
    "\n",
    "df_student_all = pd.concat(df_student_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2b24e366",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>电话</th>\n",
       "      <th>班级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三</td>\n",
       "      <td>11111</td>\n",
       "      <td>一班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>高飞</td>\n",
       "      <td>11117</td>\n",
       "      <td>一班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>里斯</td>\n",
       "      <td>11112</td>\n",
       "      <td>二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>王五</td>\n",
       "      <td>11113</td>\n",
       "      <td>二班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>马路</td>\n",
       "      <td>11114</td>\n",
       "      <td>三班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>枸杞</td>\n",
       "      <td>11115</td>\n",
       "      <td>四班</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>走吧</td>\n",
       "      <td>11116</td>\n",
       "      <td>五班</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名     电话  班级\n",
       "0  张三  11111  一班\n",
       "1  高飞  11117  一班\n",
       "0  里斯  11112  二班\n",
       "1  王五  11113  二班\n",
       "0  马路  11114  三班\n",
       "0  枸杞  11115  四班\n",
       "0  走吧  11116  五班"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_student_all"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eeda7c5d",
   "metadata": {},
   "source": [
    "### 数据合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d7e77b1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_merge = pd.merge(\n",
    "    left = df_total,\n",
    "    right = df_student_all,\n",
    "    left_on = [\"班级\", \"姓名\"],\n",
    "    right_on = [\"班级\", \"姓名\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "08c6c34c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>成绩</th>\n",
       "      <th>班级</th>\n",
       "      <th>电话号码</th>\n",
       "      <th>电话</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>张三</td>\n",
       "      <td>90</td>\n",
       "      <td>一班</td>\n",
       "      <td>None</td>\n",
       "      <td>11111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>里斯</td>\n",
       "      <td>80</td>\n",
       "      <td>二班</td>\n",
       "      <td>None</td>\n",
       "      <td>11112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>王五</td>\n",
       "      <td>78</td>\n",
       "      <td>二班</td>\n",
       "      <td>None</td>\n",
       "      <td>11113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>马路</td>\n",
       "      <td>98</td>\n",
       "      <td>三班</td>\n",
       "      <td>None</td>\n",
       "      <td>11114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>枸杞</td>\n",
       "      <td>96</td>\n",
       "      <td>四班</td>\n",
       "      <td>None</td>\n",
       "      <td>11115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>走吧</td>\n",
       "      <td>95</td>\n",
       "      <td>五班</td>\n",
       "      <td>None</td>\n",
       "      <td>11116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>高飞</td>\n",
       "      <td>88</td>\n",
       "      <td>一班</td>\n",
       "      <td>None</td>\n",
       "      <td>11117</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   学号  姓名  成绩  班级  电话号码     电话\n",
       "0   1  张三  90  一班  None  11111\n",
       "1   2  里斯  80  二班  None  11112\n",
       "2   3  王五  78  二班  None  11113\n",
       "3   4  马路  98  三班  None  11114\n",
       "4   5  枸杞  96  四班  None  11115\n",
       "5   6  走吧  95  五班  None  11116\n",
       "6   7  高飞  88  一班  None  11117"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_merge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6064dafe",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_merge[\"电话号码\"] = df_merge[\"电话\"]\n",
    "df_merge.drop(columns=\"电话\", inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a6d88c4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>姓名</th>\n",
       "      <th>成绩</th>\n",
       "      <th>班级</th>\n",
       "      <th>电话号码</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>张三</td>\n",
       "      <td>90</td>\n",
       "      <td>一班</td>\n",
       "      <td>11111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>里斯</td>\n",
       "      <td>80</td>\n",
       "      <td>二班</td>\n",
       "      <td>11112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>王五</td>\n",
       "      <td>78</td>\n",
       "      <td>二班</td>\n",
       "      <td>11113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>马路</td>\n",
       "      <td>98</td>\n",
       "      <td>三班</td>\n",
       "      <td>11114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>枸杞</td>\n",
       "      <td>96</td>\n",
       "      <td>四班</td>\n",
       "      <td>11115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>走吧</td>\n",
       "      <td>95</td>\n",
       "      <td>五班</td>\n",
       "      <td>11116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>高飞</td>\n",
       "      <td>88</td>\n",
       "      <td>一班</td>\n",
       "      <td>11117</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   学号  姓名  成绩  班级   电话号码\n",
       "0   1  张三  90  一班  11111\n",
       "1   2  里斯  80  二班  11112\n",
       "2   3  王五  78  二班  11113\n",
       "3   4  马路  98  三班  11114\n",
       "4   5  枸杞  96  四班  11115\n",
       "5   6  走吧  95  五班  11116\n",
       "6   7  高飞  88  一班  11117"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_merge"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2022d4a",
   "metadata": {},
   "source": [
    "### 更新Excel表格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ecb5d49a",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook.sheets[0].range(\"A1\").options(index=False).value = df_merge"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d2ed72",
   "metadata": {},
   "source": [
    "### 保存表格关闭程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c6ba070e",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook.save()\n",
    "workbook.close()\n",
    "app.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af99bfb8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
